<template>
<div class="app-container calendar-list-container">
  <div class="filter-container">
    <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="姓名或账户" v-model="listQuery.name"> </el-input>
    <el-button class="filter-item" type="primary" v-waves icon="search" @click="handleFilter">搜索</el-button>
    <el-button class="filter-item"  v-if="purchaseOrder_btn_add"  style="margin-left: 10px;" @click="handleCreate" type="primary" icon="edit">添加</el-button>
  </div>
  <el-table :key='tableKey' :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
    <el-table-column align="center" label="序号" width="65"> <template scope="scope">
          <span>{{scope.row.id}}</span>
        </template> </el-table-column>
    <el-table-column width="200" align="center" label="单据编号"> <template scope="scope">
        <span>{{scope.row.单据编号}}</span>
      </template> </el-table-column>
    <el-table-column width="110" align="center" label="单据日期"> <template scope="scope">
            <span>{{scope.row.单据日期}}</span>
          </template> </el-table-column>
    <el-table-column width="110" align="center" label="请购机构"> <template scope="scope">
            <span>{{scope.row.请购机构}}</span>
          </template> </el-table-column>
    <el-table-column width="300" align="center" label="收货科室"> <template scope="scope">
            <span>{{scope.row.收货科室}}</span>
          </template> </el-table-column>
    <el-table-column width="180" align="center" label="供应商"> <template scope="scope">
          <span>{{scope.row.供应商}}</span>
        </template> </el-table-column>
    <el-table-column width="200" align="center" label="制单人"> <template scope="scope">
            <span>{{scope.row.制单人}}</span>
          </template> </el-table-column>
    <el-table-column align="center" label="操作" width="230"> <template scope="scope">
      <el-button v-if="purchaseOrder_btn_approval" size="small" type="success" @click="handleApproval(scope.row)">提交审批
      </el-button>
        <el-button v-if="purchaseOrder_btn_edit" size="small" type="success" @click="handleUpdate(scope.row)">编辑
        </el-button>
        <el-button v-if="purchaseOrder_btn_del" size="small" type="danger" @click="handleDelete(scope.row)">删除
        </el-button>
      </template> </el-table-column>
  </el-table>
  <div v-show="!listLoading" class="pagination-container">
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
  </div>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" class="el-dialog--large">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">
      <el-form-item label="请购机构" prop="请购机构">
        <el-input v-model="form.请购机构" placeholder="请输入请购机构"></el-input>
      </el-form-item>
      <el-form-item label="收货科室" prop="收货科室">
        <el-input v-if="dialogStatus == 'create'" v-model="form.收货科室" placeholder="请输入收货科室"></el-input>
        <el-input v-else v-model="form.收货科室" placeholder="请输入收货科室" readonly></el-input>
      </el-form-item>
      <el-form-item label="供应商" prop="供应商">
        <el-input v-if="dialogStatus == 'create'" v-model="form.供应商" placeholder="请输入供应商"></el-input>
        <el-input v-else v-model="form.供应商" placeholder="请输入供应商" readonly></el-input>
      </el-form-item>
      <el-form-item label="制单人" prop="制单人">
        <el-input v-if="dialogStatus == 'create'" v-model="form.制单人" placeholder="请输入制单人"></el-input>
        <el-input v-else v-model="form.制单人" placeholder="请输入制单人" readonly></el-input>
      </el-form-item>
      <el-button class="filter-item"   style="margin-left: 10px;" @click="addGoods" type="primary" icon="edit">添加商品</el-button>
      <el-table :data="form.items" class="tb-edit" style="width: 100%" highlight-current-row @row-click="handleCurrentChange2">
        <el-table-column type="index" align="center" label="序号" width="50">
          <!--<template scope="scope">
          <span>{{scope.$index}}</span>
        </template> -->
        </el-table-column>
        <el-table-column label="商品" width="180">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.商品" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.商品}}</span>
          </template>
        </el-table-column>
        <el-table-column label="单价" width="100">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.单价" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.单价}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="单位" label="单位">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.单位" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.单位}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="申购数量" label="申购数量">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.申购数量" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.申购数量}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="金额" label="金额">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.金额" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.金额}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="税率" label="税率">
          <template scope="scope">
            <el-input size="small" v-model="scope.row.税率" placeholder="请输入内容" @change="handleEdit2(scope.$index, scope.row)"></el-input>
            <span>{{scope.row.税率}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <!--<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
            <el-button size="small" type="danger" @click="delGoods(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create2('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update2('form')">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
import {
  page,
  addObj,
  getObj,
  delObj,
  putObj,
  addPurchase,
  getPurchase,
  putPurchase,
  commitApproval
} from 'api/medical/purchase/index';
import { mapGetters } from 'vuex';
export default {
  name: 'user',
  data() {
    return {
      form: {
        单据编号: undefined,
        单据日期: undefined,
        请购机构: undefined,
        收货科室: undefined,
        供应商: undefined,
        制单人: undefined
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入用户',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        username: [
          {
            required: true,
            message: '请输入账户',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 20,
            message: '长度在 5 到 20 个字符',
            trigger: 'blur'
          }
        ]
      },
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined
      },
      sexOptions: ['男', '女'],
      dialogFormVisible: false,
      dialogStatus: '',
      purchaseOrder_btn_approval: false,
      purchaseOrder_btn_edit: false,
      purchaseOrder_btn_del: false,
      purchaseOrder_btn_add: false,
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableKey: 0
    }
  },
  created() {
    this.getList();
    this.purchaseOrder_btn_approval = true; /* this.elements['purchaseOrder:btn_approval'];*/
    this.purchaseOrder_btn_edit = this.elements['purchaseOrder:btn_edit'];
    this.purchaseOrder_btn_del = this.elements['purchaseOrder:btn_del'];
    this.purchaseOrder_btn_add = this.elements['purchaseOrder:btn_add'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getList() {
      this.listLoading = true;
      page(this.listQuery)
        .then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
    handleFilter() {
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    handleCreate() {
      this.resetTemp();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    handleUpdate(row) {
      getPurchase(row.id)
        .then(response => {
          this.form = response.data;
          this.dialogFormVisible = true;
          this.dialogStatus = 'update';
        });
    },
    handleApproval(row) {
      commitApproval(row.id)
        .then(() => {
          this.getList();
          this.$notify({
            title: '成功',
            message: '提交成功',
            type: 'success',
            duration: 2000
          });
        });
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delObj(row.id)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        });
    },
    create(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addObj(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
            })
        } else {
          return false;
        }
      });
    },
    create2(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          addPurchase(this.form)
            .then(() => {
              this.dialogFormVisible = false;
              this.getList();
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success',
                duration: 2000
              });
            })
        } else {
          return false;
        }
      });
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    update(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          this.form.password = undefined;
          putObj(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    update2(formName) {
      const set = this.$refs;
      set[formName].validate(valid => {
        if (valid) {
          this.dialogFormVisible = false;
          putPurchase(this.form.id, this.form).then(() => {
            this.dialogFormVisible = false;
            this.getList();
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            });
          });
        } else {
          return false;
        }
      });
    },
    resetTemp() {
      this.form = {
        请购机构: undefined,
        收货科室: undefined,
        供应商: undefined,
        制单人: undefined,
        items: [{ index: undefined, 商品: undefined, 单价: undefined, 单位: undefined, 申购数量: undefined, 金额: undefined, 税率: undefined }]
      };
    },
    handleCurrentChange2(row, event, column) {
      console.log(row, event, column, event.currentTarget)
    },
    handleEdit2(index, row) {
      console.log(index, row);
    },
    handleDelete2(index, row) {
      console.log(index, row);
    },
    addGoods() {
      for (const i of this.form.items) {
        if (i.isSet) return app.$message.warning('请先保存当前编辑项');
      }
      const j = { id: 0, 商品: '', 单价: '', 单位: '', 申购数量: '', 金额: '', 税率: '' };
      this.form.items.push(j);
      this.form.items.sel = JSON.parse(JSON.stringify(j));
    },
    delGoods(index, row) {
      this.form.items.splice(index, 1);
    }
  }
}
</script>
<style>
  * {
    margin: 0;
    padding: 0
  }
  body {
    font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
    overflow: auto;
    font-weight: 400;
    -webkit-font-smoothing: antialiased;
  }
  .tb-edit .el-input {
    display: none
  }
  .tb-edit .current-row .el-input {
    display: block
  }
  .tb-edit .current-row .el-input+span {
    display: none
  }
</style>
